#pragma once

//////////////////////////////////////////////////////////////////////////
//
//Created By Dengzhenbo 
//Date: 2009-3-12
//
//////////////////////////////////////////////////////////////////////////



#include <string>
#include <assert.h>

#define GetConfigValueInt GetPrivateProfileIntA
#define GetConfigValueString GetPrivateProfileStringA
#define SetConfigValueString WritePrivateProfileStringA



#define DEFINE_INT_CONFIG(Section, Key, Default) \
	int Get##Section##Key() \
	{ \
		return GetConfigValueInt(#Section, #Key, Default, m_strFileName.c_str());  \
	};  \
	int Set##Section##Key(int nValue)  \
	{  \
		char szValue[20];  \
		sprintf_s(szValue, sizeof(szValue), "%d", nValue);  \
		return SetConfigValueString(#Section, #Key, szValue, m_strFileName.c_str());  \
	};
	
#define DEFINE_STRING_CONFIG(Section, Key, Default) \
	std::string Get##Section##Key() \
	{ \
		int  nRetCode = 0; \
		char szValue[512] = {0}; \
		std::string strReturn; \
		GetConfigValueString(#Section, #Key, Default, szValue, sizeof(szValue), m_strFileName.c_str());  \
		strReturn = szValue; \
		return strReturn; \
	};  \
	int Set##Section##Key(char* szValue)  \
	{  \
		return SetConfigValueString(#Section, #Key, szValue, m_strFileName.c_str());  \
	};




class CConfigBase
{
public:

	CConfigBase()
	{
		m_strFileName = "config.ini";
	};

	CConfigBase(const char* pszFilePath)
	{
		assert(pszFilePath);
		m_strFileName = pszFilePath;
	};

	~CConfigBase(void){};

	void SetConfigFilePath(const char* pszFilePath)
	{
		assert(pszFilePath);
		m_strFileName = pszFilePath;
	}

	int GetConfigInt(const char* pszSection, const char* pszKey, int nDefaule)
	{
		return GetConfigValueInt(pszSection, pszKey, nDefaule, m_strFileName.c_str());
	};

	int SetConfigInt(const char* pszSection, const char* pszKey, int nValue)
	{
		char szValue[20]; 
		sprintf_s(szValue, sizeof(szValue), "%d", nValue);
		return SetConfigValueString(pszSection, pszKey, szValue, m_strFileName.c_str());
	};

	std::string GetConfigString(const char* pszSection, const char* pszKey, const char* pszDefaule) //String Value Max Length = 512
	{
		int  nRetCode = 0;
		char szValue[512] = {0};
		std::string strReturn;
		GetConfigValueString(pszSection, pszKey, pszDefaule, szValue, sizeof(szValue), m_strFileName.c_str());
		strReturn = szValue;
		return strReturn;
	};

	int SetConfigString(const char* pszSection, const char* pszKey, const char* pszValue)
	{		
		return SetConfigValueString(pszSection, pszKey, pszValue, m_strFileName.c_str());
	};
	
	/*
	//Sample to Add Config

	DEFINE_INT_CONFIG(Server, Port, 1);	
	DEFINE_STRING_CONFIG(Server, Address, "AA");	
	*/


protected:

	std::string m_strFileName;


};
